package com.brillilab.starter.controller.aio.kit;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.po.kit.KitRegularUsageGroup;
import com.brillilab.domain.vo.kit.KitRegularUsageList;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.logic.kit.KitRegularUsageLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import com.github.pagehelper.util.StringUtil;

/**
 * 常用清单Controller
 */
@RestController
@RequestMapping(value="/api/regularUsage")
public class KitRegularUsageController {

    @Autowired
    private UsersLogic usersLogic;
    @Autowired
    private KitRegularUsageLogic kitRegularUsageLogic;

    /**
     * 常用取用列表
     * @return
     */
    @RequestMapping(value="/list",method=RequestMethod.GET)
    public ResponseVo getRegularUsageList(@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="10")Integer pageSize,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        PageVo<KitRegularUsageList> result = kitRegularUsageLogic.getRegularUsageList(userInfo.getLabId(),userInfo.getLabMemberId(),pageNum,pageSize);

        return ResponseVo.success(result);
    }

    /**
     * 保存取用清单为常用
     * @return
     */
    @AspectLog
    @RequestMapping(value="/add",method=RequestMethod.GET)
    public ResponseVo addUsageToRegularList(String usageTime ,@RequestHeader(name=StarterConstant.TOKEN) String token){


        Assert.isTrue(usageTime!=null && !StringUtil.isEmpty(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        kitRegularUsageLogic.addUsageToRegularList(userInfo,usageTime);

        return ResponseVo.success();
    }

    /**
     * 常用清单再次取用
     * @return
     */
    @AspectLog
    @RequestMapping(value="/take",method=RequestMethod.POST)
    public ResponseVo takeAgain(@RequestBody JSONObject jsonObject ,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Long groupId=jsonObject.getLong("groupId");
        Assert.isTrue(groupId!=null && !StringUtil.isEmpty(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        kitRegularUsageLogic.takeAgain(groupId,userInfo.getLabId(),userInfo.getLabMemberId());

        return ResponseVo.success();
    }

    /**
     * 删除常用清单
     * @return
     */
    @AspectLog
    @RequestMapping(value="/delete",method=RequestMethod.GET)
    public ResponseVo deleteRegularUsage(String groupId ,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(groupId!=null && !StringUtil.isEmpty(token),"请求参数缺失！");

        Long gid=Long.valueOf(groupId.trim());

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        kitRegularUsageLogic.deleteRegularUsage(userInfo.getLabId(),userInfo.getLabMemberId(),gid);

        return ResponseVo.success();
    }

    /**
     * 修改常用清单名称
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/rename",method=RequestMethod.GET)
    public ResponseVo renameRegularUsage(String groupId,String name,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Assert.isTrue(groupId!=null && !StringUtils.isBlank(name) && !StringUtils.isBlank(token),"请求参数缺失！");

        Long gId = Long.valueOf(groupId);

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        KitRegularUsageGroup group = kitRegularUsageLogic.renameRegularUsage(userInfo.getLabId(),userInfo.getLabMemberId(),gId,name);

        return ResponseVo.success(group);
    }
}
